package com.example.zhinongbao.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.zhinongbao.R;
import com.example.zhinongbao.contract.SchoolContract;
import com.example.zhinongbao.presenter.SchoolPresenter;

public class PublishNewsActivity extends AppCompatActivity implements SchoolContract.View {
    private static final int REQUEST_IMAGE_PICK = 101;

    private EditText etNewsTitle;
    private EditText etNewsContent;
    private ImageView ivNewsImage;
    private Button btnSelectImage;
    private Button btnPublish;

    private SchoolPresenter presenter;
    private String imagePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_news);

        // 初始化视图
        initViews();

        // 初始化Presenter
        presenter = new SchoolPresenter(this);
        presenter.attachView(this);
    }

    private void initViews() {
        etNewsTitle = findViewById(R.id.et_news_title);
        etNewsContent = findViewById(R.id.et_news_content);
        ivNewsImage = findViewById(R.id.iv_news_image);
        btnSelectImage = findViewById(R.id.btn_select_image);
        btnPublish = findViewById(R.id.btn_publish);

        // 图片选择按钮
        btnSelectImage.setOnClickListener(v -> selectImage());

        // 发布按钮
        btnPublish.setOnClickListener(v -> attemptPublish());
    }

    private void selectImage() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_IMAGE_PICK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                imagePath = selectedImageUri.toString();
                Glide.with(this)
                        .load(selectedImageUri)
                        .placeholder(R.drawable.ic_image_placeholder)
                        .error(R.drawable.ic_broken_image)
                        .into(ivNewsImage);
            }
        }
    }

    private void attemptPublish() {
        String title = etNewsTitle.getText().toString().trim();
        String content = etNewsContent.getText().toString().trim();

        // 验证输入
        if (TextUtils.isEmpty(title)) {
            showToast("请输入新闻标题");
            return;
        }

        if (TextUtils.isEmpty(content)) {
            showToast("请输入新闻内容");
            return;
        }

        // 显示加载状态
        showLoading(true);

        // 调用Presenter发布
        presenter.publishNews(title, content, imagePath);
    }

    @Override
    public void showSchoolNews(String[] newsTitles, String[] htmlContents, String[] imageUrls) {
        // 在PublishActivity中不需要实现
    }

    @Override
    public void showError(String message) {
        runOnUiThread(() -> {
            showLoading(false);
            showToast(message);
        });
    }

    @Override
    public void onPublishSuccess() {
        runOnUiThread(() -> {
            showLoading(false);
            showToast("发布成功");
            finish(); // 返回上一页
        });
    }

    private void showLoading(boolean isLoading) {
        btnPublish.setEnabled(!isLoading);
        btnSelectImage.setEnabled(!isLoading);
        // 可以在这里添加ProgressBar显示
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}